package com.qf.lanzhou.blog.api.controller;

import com.alibaba.fastjson.JSON;
import com.qf.lanzhou.blog.api.pojo.entity.Collect;
import com.qf.lanzhou.blog.api.pojo.entity.User;
import com.qf.lanzhou.blog.api.service.CollectCountService;
import com.qf.lanzhou.blog.api.service.impl.CollectCountServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author jian  用户收藏文章统计
 * @Date 2020/12/17 15:51
 * @JDK 1.8
 * @Version 1.0
 */
@WebServlet("/collectcount.do")
public class CollectCountServlet extends BaseServletPost{
    private CollectCountService collectCountService=new CollectCountServiceimpl();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决请求的乱码
        req.setCharacterEncoding("utf-8");
        //解决响应乱码
        resp.setContentType("text/html;charset=utf-8");

        //后台查询,有多少个用户id,表示该用户收藏了多少篇文章
        User user1 = (User) req.getSession().getAttribute("user");
        Long id = user1.getId();
        //转为string类型
        String userId = id + "";
        Collect collect=collectCountService.selectcount(userId);
        String count = collect.getCount();
        successResultWrite(count,resp);
//        String json = JSON.toJSONString(count);
//        //把收藏数写回前端
//        resp.getWriter().write(json);
    }
}
